package cn.wubo.mybatis.plus.dynamic.datasource.demo.controller;

import cn.wubo.mybatis.plus.dynamic.datasource.demo.core.ChangeDataSourceService;
import cn.wubo.mybatis.plus.dynamic.datasource.demo.po.ConfigItemPO;
import cn.wubo.mybatis.plus.dynamic.datasource.demo.service.ConfigItemService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import com.google.common.collect.ImmutableMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

@RequestMapping(value = "/demo")
@RestController
public class DemoComtroller {

    @Autowired
    ChangeDataSourceService changeDataSourceService;

    @Autowired
    ConfigItemService configItemService;

    @RequestMapping(value = "/mutiQuery")
    public Object mutiQuery() {

        if (changeDataSourceService.changeDS("33")) System.out.println("切换到33数据库");
        List<ConfigItemPO> configItemPOList2 = configItemService.selectList(new QueryWrapper<>());

        if (changeDataSourceService.changeDS("174")) System.out.println("切换到174数据库");
        List<ConfigItemPO> configItemPOList3 = configItemService.selectList(new QueryWrapper<>());

        return new ImmutableMap
                .Builder<String, Object>()
                .put("list2", configItemPOList2.size())
                .put("list3", configItemPOList3.size())
                .build();
    }

}
